package com.chaber.containers.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Scope;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;

import com.chaber.containers.beans.ProductCategoryData;
import com.chaber.containers.beans.ProductData;
import com.chaber.containers.beans.searchcriteria.ProductSearchCriteria;
import com.chaber.containers.presenters.MessagePresenter;
import com.chaber.containers.presenters.ProdCatAddModRemPresenter;
import com.chaber.containers.presenters.ProdCatListPresenter;
import com.chaber.containers.presenters.ProductAddModRemPresenter;
import com.chaber.containers.presenters.ProductListPresenter;
import com.chaber.containers.services.ProductCategoryService;
import com.chaber.containers.services.ProductService;
import com.chaber.containers.views.MainView;
import com.chaber.containers.views.MessageView;
import com.chaber.containers.views.ProdCatAddModRemView;
import com.chaber.containers.views.ProdCatListView;
import com.chaber.containers.views.ProductAddModRemView;
import com.chaber.containers.views.ProductListView;

@Configuration
@Import({DatabaseConfigurator.class, AspectsConfigurator.class})
public class ProductManagementConfigurator {
	
	@Bean
	public PropertyPlaceholderConfigurer propertyPlaceholderConfigurer() {
		PropertyPlaceholderConfigurer configurer = new PropertyPlaceholderConfigurer();
		configurer.setLocations(new Resource[]{new ClassPathResource("application_properties.properties")});
		
		return configurer;
	}
	
	@Bean
	public MessageSource messageSource() {
		ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
		messageSource.setBasename("classpath:locale/messages_en.properties");
		messageSource.setDefaultEncoding("UTF-8");
	    
	    return messageSource;
	}
	
	@Bean
	@Autowired
	public LocalValidatorFactoryBean validator(MessageSource messageSource) {
		LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
		validator.setValidationMessageSource(messageSource);
		return validator;
	}
	
	//models
	@Bean
	@Scope(value = BeanDefinition.SCOPE_PROTOTYPE)
	public ProductData product() {
		return new ProductData();
	}
	
	@Bean
	@Scope(value = BeanDefinition.SCOPE_PROTOTYPE)
	public ProductCategoryData productCategory() {
		return new ProductCategoryData();
	}
	
	@Bean
	@Scope(value = BeanDefinition.SCOPE_PROTOTYPE)
	public ProductSearchCriteria productSearchCriteria() {
		return new ProductSearchCriteria();
	}
	
	//services
	@Bean
	public ProductService productService() {
		return new ProductService();
	}
	
	@Bean
	public ProductCategoryService productCategoryService() {
		return new ProductCategoryService();
	}
	
	//views
	@Bean
	public MainView mainView() {
		return new MainView();
	}
	
	@Bean
	public ProductListView productListView() {
		return new ProductListView();
	}
	
	@Bean
	public ProdCatListView prodCatListView() {
		return new ProdCatListView();
	}
	
	@Bean
	public ProductAddModRemView productAddModRemView() {
		return new ProductAddModRemView("Product details");
	}
	
	@Bean
	public ProdCatAddModRemView prodCatAddModRemView() {
		return new ProdCatAddModRemView("Product category details");
	}
	
	@Bean
	public MessageView messageView() {
		return new MessageView();
	}
	
	//presenters
	@Bean
	public ProductListPresenter productListPresenter() {
		return new ProductListPresenter();
	}
	
	@Bean
	public ProdCatListPresenter prodCatListPresenter() {
		return new ProdCatListPresenter();
	}
	
	@Bean
	public ProductAddModRemPresenter productAddModRemPresenter() {
		return new ProductAddModRemPresenter();
	}
	
	@Bean
	public ProdCatAddModRemPresenter prodCatAddModRemPresenter() {
		return new ProdCatAddModRemPresenter();
	}
	
	@Bean
	public MessagePresenter messagePresenter() {
		return new MessagePresenter();
	}
}
